package com.wj.facedetect.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.wj.facedetect.R;
import com.wj.facedetect.activity.base.BaseActivity;
import com.wj.facedetect.bean.home.RefactorBean;
import com.wj.facedetect.net.ReqUrls;
import com.wj.facedetect.util.CommonUtils;
import com.wj.facedetect.util.PreferencesUtils;

import java.math.BigDecimal;
import java.util.List;

public class FaceCheckResult2Activity extends BaseActivity {
    public static final String TAG = FaceCheckResult2Activity.class.getName();
    private RecyclerView recyclerView;
    private TextView tvSize;
    private SimpleDraweeView simpleDraweeViewHead;
    private TextView tvKuName;
    private TextView tvLeft;
    private FaceCheckResultAdapter adapter;
    private RefactorBean refactorBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_face_check_result2);
        refactorBean = (RefactorBean) getIntent().getSerializableExtra("refactorBean");
        initView();
    }

    private void initView() {
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        tvSize = (TextView) findViewById(R.id.tv_size);
        tvKuName = (TextView) findViewById(R.id.tv_ku_name);
        simpleDraweeViewHead = (SimpleDraweeView) findViewById(R.id.iv_head_1);
        tvLeft = (TextView) findViewById(R.id.tv_left);
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tvKuName.setText(PreferencesUtils.getInstance().getSharePreStr(PreferencesUtils.KU_ID_NAME));
        if (refactorBean != null) {
            List<RefactorBean.ResultsBean> results = refactorBean.getResults();
            if (results != null && !results.isEmpty()) {
                tvSize.setText("检索到" + results.size() + "个结果");
                String photoUrl = CommonUtils.StringToBase64(CommonUtils.trimPhotoAtdefault(refactorBean.getFace_image_uri())).replaceAll("\n", "");
                //photoUrl = photoUrl.replaceAll("\n", "");
                String uri = PreferencesUtils.getInstance().getSharePreStrBaseUrl(PreferencesUtils.BASE_URL)
                        + String.format(ReqUrls.URL_GET_PHOTO2, PreferencesUtils.getInstance().getSharePreStrClusterId(PreferencesUtils.CLUSTER_ID), photoUrl);
                simpleDraweeViewHead.setImageURI(uri);
                Log.i(TAG, "------" + uri);
            }
        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        adapter = new FaceCheckResultAdapter();
        recyclerView.setAdapter(adapter);
    }

    class FaceCheckResultAdapter extends RecyclerView.Adapter<FaceCheckResultAdapter.MyViewHolder> {

        @Override
        public void onBindViewHolder(FaceCheckResultAdapter.MyViewHolder holder, final int position) {
            if (refactorBean != null && refactorBean.getResults() != null && !refactorBean.getResults().isEmpty()) {
                RefactorBean.ResultsBean resultBean = refactorBean.getResults().get(position);
                String photoUrl = CommonUtils.StringToBase64(CommonUtils.trimPhotoAtdefault(resultBean.getFace_image_uri())).replaceAll("\n", "");
                holder.headView1.setImageURI(
                        PreferencesUtils.getInstance().getSharePreStrBaseUrl(PreferencesUtils.BASE_URL)
                                + String.format(ReqUrls.URL_GET_PHOTO2, PreferencesUtils.getInstance().getSharePreStrClusterId(PreferencesUtils.CLUSTER_ID), photoUrl));
                holder.tvName.setText(resultBean.getName());
                String personId = resultBean.getPerson_id();
                if (personId != null) {
                    holder.tvIdCardNo.setText(personId);
                }
                holder.tvKuName.setText(PreferencesUtils.getInstance().getSharePreStr(PreferencesUtils.KU_ID_NAME));
                double similarity = resultBean.getSimilarity();
                BigDecimal bigDecimal = new BigDecimal(similarity);
                int suitability = PreferencesUtils.getInstance().getSharePreSuitability(PreferencesUtils.SUITABILITY);
                if (similarity >= suitability) {
                    holder.itemView.setVisibility(View.VISIBLE);
                } else {
                    holder.itemView.setVisibility(View.GONE);
                }
                similarity = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
                holder.tvSim.setText(String.valueOf(similarity));
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(FaceCheckResult2Activity.this, FaceCheckDetailActivity.class);
                        intent.putExtra("refactorBean", refactorBean);
                        intent.putExtra("position", position);
                        startActivity(intent);
                    }
                });
            }
        }

        @Override
        public FaceCheckResultAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View inflate = null;
            inflate = LayoutInflater.from(FaceCheckResult2Activity.this).inflate(R.layout.facecheckresult2_item, parent, false);
            return new FaceCheckResultAdapter.MyViewHolder(inflate);
        }

        @Override
        public int getItemCount() {
            if (refactorBean != null && refactorBean.getResults() != null) {
                return refactorBean.getResults().size();
            } else {
                return 0;
            }
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            private SimpleDraweeView headView1;
            private TextView tvName, tvSim, tvIdCardNo, tvKuName;

            public MyViewHolder(View itemView) {
                super(itemView);
                headView1 = itemView.findViewById(R.id.iv_head_1);
                tvName = itemView.findViewById(R.id.name);
                tvSim = itemView.findViewById(R.id.sim);
                tvIdCardNo = itemView.findViewById(R.id.tv_idcardno);
                tvKuName = itemView.findViewById(R.id.tv_ku_name);
            }
        }
    }
}
